library UnitLeaderGunshipSupport initializer init requires LegacySystem

	private function GunshipClearance takes nothing returns nothing
		if GetUnitAbilityLevel(GetEnumUnit(),'A00A')<1 then
			call RemoveUnit(GetEnumUnit())
		endif
	endfunction

	private function RemoveUnitUnpiloted takes nothing returns nothing
		if getBoolean(GetEnumUnit(),"pilot")==false then
			call RemoveUnit(GetEnumUnit())
		endif
	endfunction

	private function RequestGunshipSupportConditions takes nothing returns boolean
		return GetSpellAbilityId()=='A01Z'
	endfunction

	private function RequestGunshipSupport takes nothing returns nothing
		local location l = GetSpellTargetLoc()
		local location l2 = SelectPoint(l,2000,3000)
		local real x = GetLocationX(l2)
		local real y = GetLocationY(l2)
		local real x2 = GetLocationX(l)
		local real y2 = GetLocationY(l)
		local unit u
		local unit u2
		local unit u3
		local player p = GetOwningPlayer(GetTriggerUnit())
		local group g = CreateGroup()
		call ALICEText(5.0,"|cfff3ad00ALICE|r : Gunships are approaching target." )
		call PolledWait(2.)
		call PingMinimapEx(GetLocationX(l),GetLocationY(l),5.0,255,0,0,false)
		set udg_soundpath = "war3mapImported\\AirSupport.mp3"
				call PlaySoundPath.execute()
		call PolledWait( GetRandomReal(2.0, 12.0) )
		if udg_LostCause then
			set u = CreateUnit(p,'h001',x,y,0.0) //Apache
			call GroupAddUnitSimple(u,g)
		else
			set u = CreateUnit(Player(11),'h001',x,y,0.0) //Apache
			call GroupAddUnitSimple(u,g)
			call TriggerSleepAction(0.)
			set u2 = CreateUnit(Player(11),'hgyr',x+280.0,y-280.0,0.0) //AH-1 Cobra
			call GroupAddUnitSimple(u2,g)
			call TriggerSleepAction(0.)
			set u3 = CreateUnit(Player(11),'hgyr',x-280.0,y+280.0,0.0) //AH-1 Cobra
			call GroupAddUnitSimple(u3,g)
		endif
		call IssuePointOrder(u,"attack",x2+GetRandomReal(-500.0,500.0),y2+GetRandomReal(-500.0,500.0))
		call IssuePointOrder(u2,"attack",x2+GetRandomReal(-500.0,500.0),y2+GetRandomReal(-500.0,500.0))
		call IssuePointOrder(u3,"attack",x2+GetRandomReal(-500.0,500.0),y2+GetRandomReal(-500.0,500.0))
		call PolledWait(20.)
		call IssuePointOrder(u,"attack",x2+GetRandomReal(-500.0,500.0),y2+GetRandomReal(-500.0,500.0))
		call IssuePointOrder(u2,"attack",x2+GetRandomReal(-500.0,500.0),y2+GetRandomReal(-500.0,500.0))
		call IssuePointOrder(u3,"attack",x2+GetRandomReal(-500.0,500.0),y2+GetRandomReal(-500.0,500.0))
		call PolledWait(20.)
		call GroupPointOrderLoc(g,"move",l2)
		call PolledWait(8.)
		call ForGroup(g,function RemoveUnitUnpiloted)
		call RemoveLocation(l)
		call RemoveLocation(l2)
		call DestroyGroup(g)
		set g = null
		set l = null
		set l2 = null
		set u = null
	endfunction


	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger(  )
		call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_SPELL_EFFECT )
		call TriggerAddCondition(t,Condition(function RequestGunshipSupportConditions))
		call TriggerAddAction(t,function RequestGunshipSupport)
	endfunction

endlibrary